pass features to doctest binary
authorAndrew Paseltiner <apaseltiner@gmail.com>
Mon, 9 Feb 2015 19:14:27 +0000 (14:14 -0500)
committerAndrew Paseltiner <apaseltiner@gmail.com>
Fri, 13 Feb 2015 13:52:42 +0000 (08:52 -0500)
src/cargo/ops/cargo_rustc/compilation.rs
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/ops/cargo_test.rs
tests/test_cargo_test.rs

index 44dbcf4d8c65bc46e5753e0187aa6844b77c64a9..3322732fe58d4d239bc39b8ab20cdea7e38834ea 100644 (file)
@@ -1,4 +1,4 @@
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
 use std::dynamic_lib::DynamicLibrary;
 use std::ffi::CString;
 use std::old_path::BytesContainer;
@@ -42,6 +42,9 @@ pub struct Compilation {
 
     /// Top-level package that was compiled
     pub package: Package,
+
+    /// Features enabled during this compilation.
+    pub features: HashSet<String>,
 }
 
 impl Compilation {
@@ -55,6 +58,7 @@ impl Compilation {
             binaries: Vec::new(),
             extra_env: HashMap::new(),
             package: pkg.clone(),
+            features: HashSet::new(),
         }
     }
 
index 4271d8cfbb6ca23d806e1ab05c5d630f2c1f2aef..d94168a16934e561fcd67adfff401d7096c4b8ad 100644 (file)
@@ -189,6 +189,11 @@ pub fn compile_targets<'a, 'b>(env: &str,
     let out_dir = cx.layout(pkg, Kind::Target).build_out(pkg)
                     .display().to_string();
     cx.compilation.extra_env.insert("OUT_DIR".to_string(), Some(out_dir));
+
+    if let Some(feats) = cx.resolve.features(pkg.package_id()) {
+        cx.compilation.features.extend(feats.iter().cloned());
+    }
+
     for (&(ref pkg, _), output) in cx.build_state.outputs.lock().unwrap().iter() {
         let any_dylib = output.library_links.iter().any(|l| {
             !l.ends_with(":static") && !l.ends_with(":framework")
index bc18f58e2cd43528bfec042eb2445b42a4843f9a..2c4ed04acc87329ecadd3003a058570d516213f7 100644 (file)
@@ -72,6 +72,10 @@ pub fn run_tests(manifest_path: &Path,
             p = p.arg("--test-args").arg(test_args.connect(" "));
         }
 
+        for feat in compile.features.iter() {
+            p = p.arg("--cfg").arg(format!("feature=\"{}\"", feat));
+        }
+
         for (pkg, libs) in compile.libraries.iter() {
             for lib in libs.iter() {
                 let mut arg = pkg.name().as_bytes().to_vec();
index 8cd266bd99ddb68ce97684bde6a1a52dbdb5e06a..715e9875dc86a27cffc220d0cdaa2c9405595455 100644 (file)
@@ -1352,3 +1352,40 @@ no example target named `foo` to run
 no bin target named `foo` to run
 "));
 });
+
+test!(doctest_feature {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+            [features]
+            bar = []
+        "#)
+        .file("src/lib.rs", r#"
+            /// ```rust
+            /// assert_eq!(foo::foo(), 1);
+            /// ```
+            #[cfg(feature = "bar")]
+            pub fn foo() -> i32 { 1 }
+        "#);
+
+    assert_that(p.cargo_process("test").arg("--features").arg("bar"),
+                execs().with_status(0).with_stdout(format!("\
+{compiling} foo [..]
+{running} target[..]foo[..]
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
+
+{doctest} foo
+
+running 1 test
+test foo_0 ... ok
+
+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
+
+", compiling = COMPILING, running = RUNNING, doctest = DOCTEST)))
+});